package com.hsgene.util.mq.rocket.annotation;

import com.aliyun.openservices.ons.api.ONSFactory;
import com.aliyun.openservices.ons.api.Producer;
import com.aliyun.openservices.ons.api.PropertyKeyConst;
import com.aliyun.openservices.shade.com.alibaba.rocketmq.client.exception.MQClientException;
import com.aliyun.openservices.shade.com.alibaba.rocketmq.client.producer.DefaultMQProducer;
import com.hsgene.util.mq.rocket.config.RocketMQConfigurationProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.Properties;

/**
 * RocketMQ配置
 *
 * @author wangbing
 * @version 1.0
 * @date 2019/1/16 10:53
 * Copyright: Copyright (c) 2019
 */
@Configuration
public class RocketMQConfiguration {

    public static final Logger LOGGER = LoggerFactory.getLogger(RocketMQConfiguration.class);

    @Resource
    private RocketMQConfigurationProperties properties;

    @SuppressWarnings("all")
    @Bean
    public Producer getRocketMQProducer() {
        Properties producerProperties = new Properties();
        producerProperties.put(PropertyKeyConst.ProducerId, properties.getProducerId());
        producerProperties.put(PropertyKeyConst.AccessKey, properties.getAccessKey());
        producerProperties.put(PropertyKeyConst.SecretKey, properties.getSecretKey());
        producerProperties.put(PropertyKeyConst.ONSAddr, properties.getNamsrvAddr());
        Producer producer = ONSFactory.createProducer(producerProperties);
        producer.start();
        return producer;
    }
}
